package com.globalwave.pos.user.service;


import junit.framework.Assert;

import org.junit.Test;

import com.globalwave.common.exception.BusinessException;
import com.globalwave.system.entity.ShopProfile;
import com.globalwave.system.entity.TerminalProfile;
import com.globalwave.system.entity.User;


public class ShopProfileServiceTest extends PosBaseTestCase {

	@Test
	public void dayBegin_OK() throws Exception {
		
		//execute(getSqlFileDir() + "TerminalProfileServiceTest_dayEnd_OK.sql") ;
		
		ShopProfile s$rt01 = getShopRT01() ;

		// close sale date is 20100930, open sale date must after 20100930
		s$rt01.setSale_date(getDate("20100930")) ;
		try {
		    getShopProfileService().dayBegin(s$rt01, getUserRT010078()) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("sale day too early!", "E0001030009", be.getMessageKey()) ;
		}		
		
		// common day begin
		s$rt01.setSale_date(getDate("20101001")) ;
		getShopProfileService().dayBegin(s$rt01, getUserRT010078()) ;
		
		ShopProfile s = getShopProfileService().get(regionCode, shopCode) ;
		
		ShopProfile exp = getShopRT01() ;
		exp.setStatus_(ShopProfile.STATUS_OPEN) ;
		exp.setSale_date(getDate("20101001")) ;
		
		assertObject("after day begin", exp, s, new String[]{"status_", "sale_date"}) ;
		
		
        // can't not open 2 times!!
		try {
			getShopProfileService().dayBegin(s$rt01, getUserRT010078()) ;

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("status is open!", "E0001030003", be.getMessageKey()) ;
		}
		
	}
	

	@Test
	public void dayEnd_OK() throws Exception {

		
		//execute(getSqlFileDir() + "TerminalProfileServiceTest_dayBegin_OK.sql") ;
		
		ShopProfile s$rt01 = getShopRT01() ;

		getShopProfileService().dayEnd(s$rt01) ;	
	    
	    ShopProfile s = getShopProfileService().get(regionCode, shopCode) ;
	    
		TerminalProfile exp = new TerminalProfile() ;
		exp.setStatus_(ShopProfile.STATUS_CLOSE) ;
		
		assertObject("after day end", exp, s, new String[]{"status_"}) ;

	    TerminalProfile t = getTerminalProfileService().get(regionCode, terminalCode) ;
	    
		TerminalProfile t_exp = new TerminalProfile() ;
		t_exp.setStatus_(ShopProfile.STATUS_CLOSE) ;
		
		assertObject("after day end", t_exp, t, new String[]{"computer_ip", "computer_name", "current_user", "status_"}) ;
		
		// can't not close 2 times!!
		try {
			getShopProfileService().dayEnd(s$rt01) ;	

			Assert.assertFalse("must be throw exception!", true) ;
			
		} catch (BusinessException be) {
			Assert.assertEquals("status is close!", "E0001030004", be.getMessageKey()) ;
			//System.err.println(be.getLocalizedMessage());
		}
	}
	
	private ShopProfile getShopRT01() throws Exception {
		ShopProfile e = new ShopProfile() ;
		e.setRegion_code(regionCode) ;
		e.setCode_(shopCode) ;
		e.setSale_date(getDate("20101001")) ;
		return e ;
	}

	
	private User getUserRT010078() {
		User e = new User() ;
		e.setRegion_code(regionCode) ;
		e.setLogin_id(userId) ;
		e.setShop_code(shopCode);

		return e ;
		
	} 
}
